package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class DVD here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVD
{
    private String id;
    private String title;
    private String artist;
    private String publicationDate;
    private ArrayList<Copy> copiesOfDvd;
    
    public DVD(String id, String title, String artist, String publicationDate)
    {
        this.id = id;
        this.title = title;
        this.artist = artist;
        this.publicationDate = publicationDate;
        copiesOfDvd = new ArrayList<Copy>();
    }
    
    
    //Creates new Copy object and instantly adds it to copiesOfDvd
    public void createCopy(String serialNumber, String purchaseDate, String purchasePrice)
    {
            copiesOfDvd.add(new Copy(serialNumber,purchaseDate,purchasePrice));
    }
    
    //Returns ArrayList of copies
    public ArrayList<Copy> copiesCollection(){
        if(copiesOfDvd == null){
            System.out.println("No copies of DVD at the time.");
        }
         return copiesOfDvd;
    }
    
    //Updates copies information
    public void updateCopies(String serialNumber, String purchaseDate, String purchasePrice){
            findCopy(serialNumber).update(purchaseDate, purchasePrice);
    }
    
    //Finds copy according it's serialNumber
    public Copy findCopy(String serialNumber) {
        for(int i=0; i<copiesOfDvd.size();i++){
            if(serialNumber.equals(copiesOfDvd.get(i).getSerialNumber())){
                return copiesOfDvd.get(i);
            }
        }
        return null;
    }
    
    //removes Copy object according serialNumber
    public void removeCopy(String serialNumber){
            copiesOfDvd.remove(findCopy(serialNumber));
    }
    public void setId(String id)
    {
        this.id = id;
    }

    public String getID()
    {
        return id;
    }
    
    public void setTitle(String title)
    {
        this.title = title;
    }

    public String getTitle()
    {
        return title;
    }
    
    public void setArtist(String artist)
    {
        this.artist = artist;
    }

    public String getArtist()
    {
        return artist;
    }
    
    public void setPublicationDate(String publicationDate)
    {
        this.publicationDate = publicationDate;
    }

    public String getPublicationDate()
    {
        return publicationDate;
    }
    //Updates information about DVD according passed values
    public void update(String title, String artist, String publicationDate)
    {
        this.title = title;
        this.artist = artist;
        this.publicationDate = publicationDate;
        
    }
    
    public String toString() // returns a string with atributes of a dvd
    {
        String s="Title=" + getTitle()+'\n'+ "Artist=" +getArtist()+ '\n' +"ID="+getID()+'\n' + "PublicationDate=" + getPublicationDate()+'\n';
        return s;
    }
    
    
    public Copy findUnrentedCopy() //finds an unrented copy in the copy collection
    {
        int index=0;
        boolean found= false;
        Copy c=null;
        while(!found && index<copiesOfDvd.size())
        {
            if ( !copiesOfDvd.get(index).isRented())
            {
            c=copiesOfDvd.get(index);
            copiesOfDvd.get(index).rentCopy();
            found=true;
            }
            index++;
        }
        if(c==null)
        System.out.println("There are no copies for dvd: " + getTitle()); 
        return c;
    }
            
}
